package com.fsc.app_news;

import java.util.ArrayList;

import com.fsc.app_bean.Recommend;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class OtherActivity extends Fragment implements OnItemClickListener, OnClickListener {
	private GridView gv_recommend;
	private ArrayList<Recommend> data;
	private Recommend list;
	private recommendAdapter adapter;
	private View view;
	
	/***************图片缩放**************/
	private ImageView iv_rContent;
	Bitmap bp=null;    
    float scaleWidth;  
    float scaleHeight;  
    int h;  
    boolean num=false;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.activity_other,container, false);
		gv_recommend = (GridView) view.findViewById(R.id.gv_recommend);
		iv_rContent = (ImageView) view.findViewById(R.id.iv_rContent);
		envet();
		//准备数据集
		data = new ArrayList<Recommend>();
		data.add(new Recommend("与每天天不亮就要起床往学校赶的大多数中国孩子不同。",R.drawable.tu1,R.drawable.tu2,R.drawable.tu3));
		data.add(new Recommend("与每天天不亮就要起床往学校赶的大多数中国孩子不同。",R.drawable.tu4,R.drawable.tu1,R.drawable.tu2));
		data.add(new Recommend("韩百川的决定很突然，刚上初一不久，他就向家人提出。",R.drawable.tu1,R.drawable.tu4,R.drawable.tu3));
		//设置baseAdapter对象
		adapter = new recommendAdapter(getActivity());
		//设置Adapter显示列表
		gv_recommend.setAdapter(adapter);
		
		/*************图片缩放*********************/
		Display display = getActivity().getWindowManager().getDefaultDisplay();
        bp=BitmapFactory.decodeResource(getResources(),R.drawable.playimg5);  
        int width=bp.getWidth();  
        int height=bp.getHeight();  
        int w=display.getWidth();  
        int h=display.getHeight();  
        scaleWidth=((float)w)/width;  
        scaleHeight=((float)h)/height;  
        iv_rContent.setImageBitmap(bp);
        
		return view;
	}
	public void envet(){
		gv_recommend.setOnItemClickListener(this);
		iv_rContent.setOnClickListener(this);
	}
	class recommendAdapter extends BaseAdapter{
		private Context context;
		
  		//构造函数
  		public recommendAdapter(Context context) {  
  	        this.context = context;
  	    }
		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			ViewHolder hodler = null; 
  	        if(convertView == null)  
  	        {  
  	        	hodler = new ViewHolder();
  	        	convertView = LayoutInflater.from(context).inflate(R.layout.item_recommend, null); 
  	            hodler.content = (TextView) convertView.findViewById(R.id.textView1); 
  	            hodler.image1 = (ImageView) convertView.findViewById(R.id.iv_image1);
  	            hodler.image2 = (ImageView) convertView.findViewById(R.id.iv_image2);
  	            hodler.image3 = (ImageView) convertView.findViewById(R.id.iv_image3);
  	            convertView.setTag(hodler);         //表示给View添加一个格外的数据，  
  	        } else {  
  	        	hodler = (ViewHolder) convertView.getTag();//通过getTag的方法将数据取出来  
  	        } 
  	      list = data.get(position); 
  	        
  	        hodler.content.setText(list.getContent());//显示标题  
  	        hodler.image1.setImageResource(list.getImage1());
  	        hodler.image2.setImageResource(list.getImage2()); 
  	        hodler.image3.setImageResource(list.getImage3()); 
  	        return convertView; 
		}
	}
	public class ViewHolder {
		private TextView content;
		private ImageView image1;
		private ImageView image2;
		private ImageView image3;
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent(getActivity(),RPageActivity.class);
		list = data.get(position);
		
		intent.putExtra("content", list.getContent());
		startActivity(intent);
	}
	@Override
	public void onClick(View v) {
		switch(v.getId()){  
        
        case R.id.iv_rContent:  
         if(num==true)        {  
             Matrix matrix=new Matrix();  
             matrix.postScale(scaleWidth,scaleHeight);  
               
             Bitmap newBitmap=Bitmap.createBitmap(bp, 0, 0, bp.getWidth(), bp.getHeight(), matrix, true);  
             iv_rContent.setImageBitmap(newBitmap);  
             Toast.makeText(getActivity(), "选中", Toast.LENGTH_SHORT).show();
             num=false;  
             }  
         else{  
             Matrix matrix=new Matrix();  
             matrix.postScale(1.0f,1.0f);  
             Bitmap newBitmap=Bitmap.createBitmap(bp, 0, 0, bp.getWidth(), bp.getHeight(), matrix, true);  
             iv_rContent.setImageBitmap(newBitmap); 
             Toast.makeText(getActivity(), "取消", Toast.LENGTH_SHORT).show();
             num=true;  
     }  
         break;  
        }  
	}
}
